#!/bin/bash
###############################  DOUPDATE  ###################################
# Script Name      :  doupdate
# Project          :  SuperServer https://code.google.com/p/superserver/
# Authors          :  Copyright (c) 2012 TWConnor twconnor@gmail.com
# Lastest Revision :  02/26/2013 20:39:53  
# Created On       :  05.02.2011 12:13:21
# Version          :  0.0.1.0
# Description      :  An update and upgrade utility for ubuntu. 
# Prerequisites    :  Ubuntu 12.04, Ubuntu 12.10, ping, 
#                     OPTIONALLY: apt-fast
# Notes            :  Must run as root.
#
#
################################# OPTIONS ####################################
#

#
###################### DO NOT EDIT BELOW THIS LINE ###########################
#
set -e
NAME=`basename $0`
LOCKFILE=/tmp/`basename $0`.lock
trap 'rm $LOCKFILE' EXIT
LOG=$NAME.log
VERSION=0.0.1.0 #
TIMESTAMP=$(date +"%D-%T") #

## Force Single Instance
function singleinstance () {
                (set -C; : > $LOCKFILE) 2> /dev/null
                if [ $? != "0" ]; then
                        echo "Lock File exists - exiting"
                        exit 1
                fi
}
## Force root priviledge.
function requireroot ()        {
                if [ $USER != root ]; then
                        echo "ERROR: root privilidges required - exiting"
                        exit 1
                fi
}
## Test if internet connection is working
function netup () {
        /bin/ping -c 1 google.com > /dev/null 2>&1 && return $YES || echo "Network NOT up" && sleep 120 && netup           
}
# Log entry
function dologentry () {
    echo $TIMESTAMP" $1" >> $LOG
}
## Do Update and Upgrade
function doupdate () {
        if [ -e /usr/sbin/apt-fast ]; then 
        /usr/sbin/apt-fast -qq -y update && /usr/sbin/apt-fast -qq -y dist-upgrade  && [ -f /var/run/reboot-required ] && /sbin/shutdown -r +30
        else 
        /usr/bin/apt-get -qq -y update && /usr/bin/apt-get -qq -y dist-upgrade  && [ -f /var/run/reboot-required ] && /sbin/shutdown -r +30
        fi
}
singleinstance
requireroot
netup
doupdate
